<template>
  <div class="update-name">
    <!-- 导航栏 -->
    <van-nav-bar title="设置昵称" left-text="取消" right-text="完成" @click-left="$emit('close')" @click-right="onConfirm">
    </van-nav-bar>
    <!-- 输入框 -->
    <div class="field-wrap">
      <van-cell-group inset>
        <van-field v-model.trim="localName" rows="2" type="textarea" maxlength="11" placeholder="请输入昵称" show-word-limit />
      </van-cell-group>
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/users.js'
export default {
  name: 'UpdateName',
  data() {
    return {
      localName: this.value
    }
  },
  props: {
    value: {
      type: String,
      required: true
    }
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, // 禁止背景点击
        duration: 0 // 持续展示
      })
      try {
        const localName = this.localName
        if (!localName) {
          this.$toast('内容不能为空')
          return
        }
        await updateUserProfile({
          name: this.localName
        })
        // console.log(data)
        this.$emit('input', this.localName)
        this.$emit('close')
        this.$toast.success('修改成功')
      } catch (err) {
        this.$toast.fail('更新昵称失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  // background-color: #3296fa;
  /deep/.van-icon {
    color: #fff;
  }
}
/deep/.van-nav-bar__title {
  color: black !important;
}
.update-name {
  .van-cell-group--inset {
    margin: 15px;
  }
}
</style>
